package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.exception.CustomException;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.ServletUtils;
import com.xjy.system.utils.DataBaseUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.SysDatabaseBackupAddBo;
import com.xjy.system.domain.bo.SysDatabaseBackupQueryBo;
import com.xjy.system.domain.bo.SysDatabaseBackupEditBo;
import com.xjy.system.domain.SysDatabaseBackup;
import com.xjy.system.mapper.SysDatabaseBackupMapper;
import com.xjy.system.domain.vo.SysDatabaseBackupVo;
import com.xjy.system.service.ISysDatabaseBackupService;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统-备份Service业务层处理
 *
 * @author xjy
 * @date 2022-03-28
 */
@Service
public class SysDatabaseBackupServiceImpl extends ServicePlusImpl<SysDatabaseBackupMapper, SysDatabaseBackup> implements ISysDatabaseBackupService {


    @Override
    public SysDatabaseBackupVo queryById(Long id){
        return getVoById(id, SysDatabaseBackupVo.class);
    }

    @Override
    public TableDataInfo<SysDatabaseBackupVo> queryPageList(SysDatabaseBackupQueryBo bo) {
        PagePlus<SysDatabaseBackup, SysDatabaseBackupVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysDatabaseBackupVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysDatabaseBackupVo> queryList(SysDatabaseBackupQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysDatabaseBackupVo.class);
    }

    private LambdaQueryWrapper<SysDatabaseBackup> buildQueryWrapper(SysDatabaseBackupQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysDatabaseBackup> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getPath()), SysDatabaseBackup::getPath, bo.getPath());
        lqw.like(StrUtil.isNotBlank(bo.getName()), SysDatabaseBackup::getName, bo.getName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo( ) {
		SysDatabaseBackup bo = new SysDatabaseBackup();
    	String path = "/usr/backup";
		String fileName = "backup_" + new Date().getTime() + ".sql";
		try {
			DataBaseUtils.exportDatabaseTool("124.222.30.251","cinema","cinema",path,fileName,"cinema");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		HttpServletRequest request = ServletUtils.getRequest();
		StringBuffer stringBufferUrl = request.getRequestURL();
		String contextPath = request.getServletContext().getContextPath();
		String url = stringBufferUrl.delete(stringBufferUrl.length() - request.getRequestURI().length(), stringBufferUrl.length()).append(contextPath).toString() + "/"+fileName;
		bo.setName(fileName);
		bo.setPath(path+"/"+fileName);

		return save(bo);
    }

    @Override
    public Boolean updateByEditBo(SysDatabaseBackupEditBo bo) {
        SysDatabaseBackup update = BeanUtil.toBean(bo, SysDatabaseBackup.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysDatabaseBackup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
